package protocol.transactions;

import java.lang.reflect.Type;
import java.util.List;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.InfoComment;
import protocol.meta.InfoForward;
import protocol.meta.InfoPraise;
import protocol.meta.PageParam;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.netease.http.THttpRequest;


public class TransactionGetInfoCfp extends XoneBaseTransaction {
    private static final int TYPE_BASE = 1;
    public static final int TYPE_COMMENT = TYPE_BASE;
    public static final int TYPE_FORWARD = TYPE_BASE + 1;
    public static final int TYPE_PRAISE = TYPE_BASE + 2;
    
    private String mInfoId;
    private int    mType;
    private PageParam mPageParam;

    public TransactionGetInfoCfp(String infoId, int type, PageParam param) {
        super(TRANSACTION_TYPE_GETINFOCFP);
        
        mInfoId = infoId;
        mType = type;
        mPageParam = param;
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj instanceof JsonElement) {
            try {
                JsonElement array = ((JsonElement)obj).getAsJsonObject().get("list");
                Gson gson = new Gson();
                switch (mType) {
                    case TYPE_COMMENT: {
                        List<InfoComment> list = null;
                        Type collectionType = new TypeToken<List<InfoComment>>(){}.getType();
                        list = gson.fromJson(array, collectionType);
                        notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, list);
                        break;
                    }
                    case TYPE_FORWARD: {
                        List<InfoForward> list = null;
                        Type collectionType = new TypeToken<List<InfoForward>>(){}.getType();
                        list = gson.fromJson(array, collectionType);
                        notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, list);
                        break;
                    }
                    case TYPE_PRAISE: {
                        List<InfoPraise> list = null;
                        Type collectionType = new TypeToken<List<InfoPraise>>(){}.getType();
                        list = gson.fromJson(array, collectionType);
                        notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, list);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @Override
    public void onTransact() {
        THttpRequest request = null;
        request = XoneProtocol.getInstance().createGetInfoCfp(mInfoId, mType, mPageParam);
        sendRequest(request);
    }
}
